package com.haiheng.voiceandbook

import android.content.Intent
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.webkit.*
import androidx.core.content.ContextCompat
import com.haiheng.bookmusic.base.BaseActivity
import com.haiheng.videodoorbell.widget.webview.IX5WebPageView
import com.haiheng.videodoorbell.widget.webview.MyX5WebChromeClient
import com.haiheng.videodoorbell.widget.webview.MyX5WebViewClient
import com.haiheng.voiceandbook.Constants.Companion.TAIZHOU_WEBVIEW_URL
import com.haiheng.voiceandbook.databinding.WebViewActivityBinding

/**
 *
 * @Des:
 * @Author: hwz
 * @Date:2022/8/8 0008
 * @Version: 1.0
 */
class WebViewActivity : BaseActivity(), IX5WebPageView {

    private lateinit var binding: WebViewActivityBinding
    override fun initDataChange() {

    }

    override fun initListener() {

    }

    override fun initData() {

        binding.progress.setColor(
            ContextCompat.getColor(this, R.color.color_ff2abcba),
            ContextCompat.getColor(this, R.color.color_ff9ff4c1)
        )
        binding.webview.webViewClient = MyX5WebViewClient(this)
        binding.webview.webChromeClient = MyX5WebChromeClient(this)
        binding.webview.loadUrl(Constants.getTaiZhouH5Url())
//        initAndSetupView()
    }

    override fun initView() {
        binding.slBack.setOnClickListener {
            if (binding?.webview.canGoBack()) {
                binding?.webview.goBack()
            }else{
                finish()
            }
        }
        binding.slClose.setOnClickListener {
            finish()
        }
    }

    override fun intiLayout() {
        binding = WebViewActivityBinding.inflate(LayoutInflater.from(this))
        setContentView(binding.root)
    }

    // 初始化对象
    /*fun initAndSetupView() {
        var webview = binding.webview
        var webSettings = webview!!.settings
        webSettings.javaScriptEnabled = true
        webSettings.javaScriptCanOpenWindowsAutomatically = true
        webSettings.allowFileAccess = true// 设置允许访问文件数据
        webSettings.setSupportZoom(true)//支持缩放
        webSettings.builtInZoomControls = true
        webSettings.javaScriptCanOpenWindowsAutomatically = true
        webSettings.javaScriptEnabled = true
        webSettings.builtInZoomControls = true
        webSettings.useWideViewPort = true
        webSettings.loadWithOverviewMode = true
        webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
        webSettings.domStorageEnabled = true
        webSettings.databaseEnabled = true
//        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN)
//        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS )
        webSettings.setUseWideViewPort(true);//设置webview显示屏幕宽度 不能滑动
        webSettings.setLoadWithOverviewMode(true);
//        webview.setInitialScale(100)
        webview!!.setOnKeyListener(OnKeyEvent)
        webview!!.setWebViewClient(webClient)
        webview!!.setWebChromeClient(object : WebChromeClient() {

        })
        webview!!.loadUrl(TAIZHOU_WEBVIEW_URL)
    }*/

    private val webClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {

            return false
        }
        override fun onPageFinished(view: WebView?, url: String?) {
            super.onPageFinished(view, url)
        }

        override fun onReceivedError(
            view: WebView?,
            request: WebResourceRequest?,
            error: WebResourceError?
        ) {
            super.onReceivedError(view, request, error)
        }
    }

    private val OnKeyEvent = View.OnKeyListener { v, keyCode, event ->
        val action = event.action
        val webView = v as WebView
        if (KeyEvent.ACTION_DOWN == action && KeyEvent.KEYCODE_BACK == keyCode) {
            if (webView?.canGoBack()) {
                webView.goBack()
                return@OnKeyListener true
            }
        }
        false
    }

    override fun showWebView() {
        binding.webview.visibility = com.tencent.smtt.sdk.WebView.VISIBLE
    }

    override fun hindWebView() {
        binding.webview.visibility = com.tencent.smtt.sdk.WebView.INVISIBLE
    }

    override fun startProgress(newProgress: Int) {
        binding.progress.setWebProgress(newProgress)
    }

    override fun startFileChooserForResult(intent: Intent?, requestCode: Int) {
        TODO("Not yet implemented")
    }

    override fun onPageFinished(view: com.tencent.smtt.sdk.WebView?, url: String?) {
        binding.progress.hide()
    }

    override fun isOpenThirdApp(url: String?): Boolean {
        if (url!!.startsWith("http")) {
            binding.webview.loadUrl(url)
        }
        return true
    }
}